package com.idudiao.sc.neo4j.controller;

import com.idudiao.sc.neo4j.model.node.Legend;
import com.idudiao.sc.neo4j.repository.LegendRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author songyinyin
 * @since 2019/1/7 14:07
 */
@Controller
@Slf4j
@AllArgsConstructor
@Api(tags = "neo4j-神话故事")
public class LegendController {

    private final LegendRepository legendRepository;

    @RequestMapping(value = "/legend/test", method = RequestMethod.GET)
    @ApiOperation("初始化legend")
    @ResponseBody
    public Object init() {
        legendRepository.deleteAll();

        Legend yangguo = new Legend("杨过");
        Legend dragonGirl = new Legend("小龙女");
        Legend guoxiang = new Legend("郭襄");

        List<Legend> team = Arrays.asList(yangguo, dragonGirl, guoxiang);
        log.info("Before linking up with Neo4j...");
        team.stream().forEach(legend -> log.info("\t" + legend.toString()));

        yangguo.fansWith(dragonGirl);
        yangguo.fansWith(guoxiang);
        dragonGirl.fansWith(guoxiang);
        legendRepository.save(yangguo);

        return yangguo;
    }

    @RequestMapping(value = "/legend/get", method = RequestMethod.GET)
    @ApiOperation("查询")
    @ResponseBody
    public Object get() {
        Optional<Legend> optional = legendRepository.findById(129L);
        return optional.get();
    }
}
